package com.alienlab.finder.web.rest;
import com.alienlab.finder.domain.CheckTask;
import com.alienlab.finder.service.CheckTaskService;
import com.alienlab.finder.service.dto.CheckTaskDTO;
import com.alienlab.finder.web.rest.errors.BadRequestAlertException;
import com.alienlab.finder.web.rest.util.HeaderUtil;
import com.alienlab.finder.web.rest.util.PaginationUtil;
import io.github.jhipster.web.util.ResponseUtil;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.net.URI;
import java.net.URISyntaxException;

import java.util.List;
import java.util.Optional;

/**
 * REST controller for managing CheckTask.
 */
@RestController
@RequestMapping("/api")
public class CheckTaskResource {

    private final Logger log = LoggerFactory.getLogger(CheckTaskResource.class);

    private static final String ENTITY_NAME = "checkTask";

    private final CheckTaskService checkTaskService;

    public CheckTaskResource(CheckTaskService checkTaskService) {
        this.checkTaskService = checkTaskService;
    }

    /**
     * POST  /check-tasks : Create a new checkTask.
     *
     * @return the ResponseEntity with status 201 (Created) and with body the new checkTask, or with status 400 (Bad Request) if the checkTask has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @ApiOperation(value ="创建新任务" )
    @PostMapping("/check-tasks")
    public ResponseEntity createCheckTask(@RequestParam String admin_open_id,@RequestParam String guest_open_id,@RequestParam String origin_x,@RequestParam String origin_y) throws URISyntaxException {
        log.debug("REST request to save CheckTask");
        if ("".equals(admin_open_id)|| admin_open_id == null) {
            throw new BadRequestAlertException("未获取到main_open_id", ENTITY_NAME, "admin_open_id do not exist");
        }
        if ("".equals(guest_open_id)|| guest_open_id == null) {
            throw new BadRequestAlertException("未获取到second_open_id", ENTITY_NAME, "guest_open_id do not exist");
        }
        if ("".equals(origin_x)|| origin_x == null) {
            throw new BadRequestAlertException("未获取到origin_x", ENTITY_NAME, "origin_x do not exist");
        }
        if ("".equals(origin_y)|| origin_y == null) {
            throw new BadRequestAlertException("未获取到origin_y", ENTITY_NAME, "origin_y do not exist");
        }
        try {
            CheckTask result = checkTaskService.creatTask(admin_open_id, guest_open_id, origin_x, origin_y);
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new Exception());
        }

    }

    @ApiOperation(value ="停止监控任务" )
    @PutMapping("/check-tasks/stop")
    public ResponseEntity stopCheckTask(@RequestParam String  task_id) throws URISyntaxException {
        log.debug("REST request to stop a CheckTask");
        if ("".equals(task_id)|| task_id == null) {
            throw new BadRequestAlertException("未获取到task_id", ENTITY_NAME, "task_id do not exist");
        }

        try {
            CheckTask result = checkTaskService.stopTask(Long.valueOf(task_id));
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new Exception());
        }

    }

    @ApiOperation(value = "获取被监控人的监控任务列表")
    @GetMapping("/check-tasks/guest")
    public ResponseEntity  getGuestCheckTasks(String guest_open_id) throws Exception {
        log.debug("REST request to get 获取被监控人的监控任务列表");
        if ("".equals(guest_open_id)||guest_open_id==null){
            throw  new Exception("未获取到guest_open_id");
        }

        List<CheckTaskDTO> checkTaskList = checkTaskService.guestChecktaskList(guest_open_id);
        return  ResponseEntity.ok(checkTaskList);
    }

    @ApiOperation(value = "获取监控人的监控任务列表")
    @GetMapping("/check-tasks/admin")
    public ResponseEntity  getAdminCheckTasks(String admin_open_id) throws Exception {
        log.debug("REST request to get 获取监控人的监控任务列表");
        if ("".equals(admin_open_id)||admin_open_id==null){
            throw  new Exception("未获取到admin_open_id");
        }

        List<CheckTaskDTO> checkTaskList = checkTaskService.adminChecktaskList(admin_open_id);
        return  ResponseEntity.ok(checkTaskList);
    }

    @ApiOperation(value = "监控人获取正在监控的被监控人列表")
    @GetMapping("/check-tasks/monitoredlist")
    public ResponseEntity  getMonitoredList(String admin_open_id) throws Exception {
        log.debug("REST request to get 监控人获取正在监控的被监控人列表");
        if ("".equals(admin_open_id)||admin_open_id==null){
            throw  new Exception("未获取到admin_open_id");
        }

        List<String> monitoredList = checkTaskService.getMonitoredList(admin_open_id);
        return  ResponseEntity.ok(monitoredList);
    }

    @ApiOperation(value = "查看两个人之间是否有正在进行的监控任务")
    @GetMapping("/check-tasks/isdoing")
    public ResponseEntity  getDoingFlag(String admin_open_id,@RequestParam String guest_open_id) throws Exception {
        log.debug("REST request to get 查看两个人之间是否有正在进行的监控任务");
        if ("".equals(admin_open_id)||admin_open_id==null){
            throw  new Exception("未获取到admin_open_id");
        }
        if ("".equals(guest_open_id)||guest_open_id==null){
            throw  new Exception("未获取到guest_open_id");
        }

        String taskId = checkTaskService.taskIsDoing(admin_open_id, guest_open_id);
        return  ResponseEntity.ok(taskId);
    }

    @ApiOperation(value = "被监控人查看否有正在进行的监控任务")
    @GetMapping("/check-tasks/guestisdoing")
    public ResponseEntity  getGuestDoingFlag(@RequestParam String guest_open_id) throws Exception {
        log.debug("REST request to get 被监控人查看否有正在进行的监控任务");

        if ("".equals(guest_open_id)||guest_open_id==null){
            throw  new Exception("未获取到guest_open_id");
        }

        String taskId = checkTaskService.getDoingtask(guest_open_id);
        return  ResponseEntity.ok(taskId);
    }
    /**
     * PUT  /check-tasks : Updates an existing checkTask.
     *
     * @param checkTask the checkTask to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated checkTask,
     * or with status 400 (Bad Request) if the checkTask is not valid,
     * or with status 500 (Internal Server Error) if the checkTask couldn't be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/check-tasks")
    public ResponseEntity<CheckTask> updateCheckTask(@Valid @RequestBody CheckTask checkTask) throws URISyntaxException {
        log.debug("REST request to update CheckTask : {}", checkTask);
        if (checkTask.getId() == null) {
            throw new BadRequestAlertException("Invalid id", ENTITY_NAME, "idnull");
        }
        CheckTask result = checkTaskService.save(checkTask);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, checkTask.getId().toString()))
            .body(result);
    }

    /**
     * GET  /check-tasks : get all the checkTasks.
     *
     * @param pageable the pagination information
     * @return the ResponseEntity with status 200 (OK) and the list of checkTasks in body
     */
    @GetMapping("/check-tasks")
    public ResponseEntity<List<CheckTask>> getAllCheckTasks(Pageable pageable) {
        log.debug("REST request to get a page of CheckTasks");
        Page<CheckTask> page = checkTaskService.findAll(pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/check-tasks");
        return ResponseEntity.ok().headers(headers).body(page.getContent());
    }

    /**
     * GET  /check-tasks/:id : get the "id" checkTask.
     *
     * @param id the id of the checkTask to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the checkTask, or with status 404 (Not Found)
     */
    @GetMapping("/check-tasks/{id}")
    public ResponseEntity<CheckTask> getCheckTask(@PathVariable Long id) {
        log.debug("REST request to get CheckTask : {}", id);
        Optional<CheckTask> checkTask = checkTaskService.findOne(id);
        return ResponseUtil.wrapOrNotFound(checkTask);
    }

    /**
     * DELETE  /check-tasks/:id : delete the "id" checkTask.
     *
     * @param id the id of the checkTask to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/check-tasks/{id}")
    public ResponseEntity<Void> deleteCheckTask(@PathVariable Long id) {
        log.debug("REST request to delete CheckTask : {}", id);
        checkTaskService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }
}
